<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="container">
          <el-alert
            title="标题前最多展示1个标签，标题下最多展示3个标签；系统根据标签顺序进行展示"
            type="warning"
            show-icon
          >
          </el-alert>
          <el-form inline style="display: block; margin-top: 5px">
            <el-form-item label="标签名称">
              <el-input v-model.trim="tableFrom.keywords" clearable placeholder="待搜索的标签名称"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button size="small" type="primary" @click="handleSearch">搜索</el-button>
            </el-form-item>
          </el-form>
          <el-button size="small" type="primary" @click="onAdd" v-hasPermi="['platform:product:brand:add']"
            >添加标签</el-button
          >
        </div>
      </div>
      <el-table
        v-loading="tableConfig.listLoading"
        :data="tableData.data"
        size="small"
        row-key="tag_id"
        :default-expand-all="false"
        :header-cell-style="{ fontWeight: 'bold' }"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column prop="id" label="ID" min-width="60" />
        <el-table-column label="标签名称" prop="tagName" min-width="100" />
        <el-table-column label="标签说明" prop="tagNote" min-width="150" />
        <el-table-column prop="sort" label="排序" min-width="50" />
        <el-table-column prop="startTime" label="生效期间" width="260">
          <template slot-scope="scope"> {{ scope.row.startTime }} - {{ scope.row.endTime }} </template>
        </el-table-column>
        <el-table-column prop="status" label="是否显示" min-width="100" fixed="right">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              :active-value="1"
              :inactive-value="0"
              active-text="显示"
              inactive-text="隐藏"
              @change="onchangeIsShow(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" min-width="150" />
        <el-table-column prop="position" label="商城标题位置" min-width="80">
          <template slot-scope="scope">
            <span>{{ scope.row.position === 0 ? '标题下' : '标题前' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="100" fixed="right">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="onEdit(scope.row)">编辑</el-button>
            <el-button
              v-if="scope.row.owner > 0"
              type="text"
              size="small"
              @click="handleDelete(scope.row.id, scope.$index)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[20, 40, 60, 80]"
          :page-size="tableFrom.limit"
          :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
    <el-drawer
      :visible.sync="editDataDialogConfig.visible"
      :title="editData.id ? '商品标签编辑' : '商品标签新增'"
      direction="rtl"
      :append-to-body="true"
      custom-class="demo-drawer"
      size="900px"
      ref="drawer"
      :wrapperClosable="editData.id ? false : true"
      :modal-append-to-body="false"
      class="infoBox"
      @close="editDataDialogConfig.visible = false"
    >
      <edit-tag
        v-if="editDataDialogConfig.visible"
        :edit-data="editData"
        @onCloseHandle="handleOnEditDiaClosed"
      ></edit-tag>
    </el-drawer>
  </div>
</template>

<script>
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
import * as storeApi from '@/api/product.js';
import editTag from './editTag';
import { mapGetters } from 'vuex';
import { checkPermi } from '@/utils/permission';
import { productTagInfoApi, productTagStatusApi } from '@/api/product.js'; // 权限判断函数
export default {
  name: 'ProductTag',
  components: {
    editTag,
  },
  computed: {},
  data() {
    return {
      isChecked: false,
      tableConfig: {
        listLoading: false,
      },
      tableData: {
        data: [],
        total: 0,
      },
      tableFrom: {
        page: 1,
        limit: 20,
        keywords: '', // 搜索关键字
      },
      editData: {},
      editDataDialogConfig: {
        visible: false,
      },
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    checkPermi,
    handleSearch() {
      this.tableFrom.page = 1;
      this.getList();
    },
    // 列表
    getList() {
      this.tableConfig.listLoading = true;
      storeApi
        .productTagListApi(this.tableFrom)
        .then((res) => {
          this.tableData.data = res.list;
          this.tableData.total = res.total;
          this.tableConfig.listLoading = false;
        })
        .catch((res) => {
          this.tableConfig.listLoading = false;
          this.$message.error(res.message);
        });
    },
    pageChange(page) {
      this.tableFrom.page = page;
      this.getList();
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val;
      this.getList();
    },
    // 添加
    onAdd() {
      this.editData = {};
      this.editDataDialogConfig.visible = true;
    },
    // 编辑
    onEdit(row) {
      this.editData = row;
      this.editDataDialogConfig.visible = true;
    },
    // 删除
    handleDelete(id, idx) {
      this.$modalSure('删除当前标签吗？').then(async () => {
        if (this.tableData.data.length === 1) {
          this.tableFrom.page = this.tableFrom.page - 1;
        }
        await storeApi.productTagDelete(id);
        this.$message.success('删除成功');
        await this.getList();
      });
    },
    onchangeIsShow(row) {
      storeApi
        .productTagStatusApi(row.id, row.status)
        .then((res) => {
          this.$message.success('操作成功');
          this.getList();
        })
        .catch((e) => {
          return (row.status = !row.status);
        });
    },
    handleOnEditDiaClosed() {
      this.editDataDialogConfig.visible = false;
      this.getList();
    },
  },
};
</script>

<style scoped lang="scss">
.selWidth {
  width: 300px !important;
}
.infoBox {
  ::v-deep.el-drawer__header {
    margin-bottom: 0;
    font-size: 20px;
  }
  ::v-deep.el-icon-arrow-down,
  ::v-deep .el-icon-arrow-up {
    display: none;
  }
}
</style>
